﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace SupervisionCodeCollection
{
    /// <summary>
    /// ClassName:UserInfo
    /// Version:1.0
    /// Date:2022-10-07
    /// Author:SeanOuyang
    /// </summary>
    /// <remarks>
    /// 本类是实体类UserInfo(操作员信息)属性说明类
    /// </remarks>
    public class UserInfo
    {
        #region 1.属性
        //操作(0:查看 1:新增 2:修改 3:删除)
        public int m_nOperate { set; get; }
        //序号
        public int m_nId { set; get; }
        //用户编号
        public string m_strCode { set; get; }
        //用户姓名
        public string m_strName { set; get; }
        //登录口令
        public string m_strPassword { set; get; }
        //用户编号
        public string m_strDeptCode { set; get; }
        //用户编号
        public string m_strDeptName { set; get; }
        //创建人
        public string m_strCreateName { set; get; }
        //创建时间
        public string m_strCreateTime { set; get; }
        //说明
        public string m_strRemark { set; get; }
        //角色
        public string m_strRole { set; get; }
        //数据
        public string m_strDataRole { set; get; }
        // 功能权限
        public Dictionary<string, string> m_dictLimit { set; get; }   
        
        #endregion

        /// <summary>
        /// 构造函数(值初始化)
        /// </summary>
        public UserInfo()
        {
            m_nId = 0;
            m_strCode = "";
            m_strName = "";
            m_strPassword = "";
            m_strDeptCode = "";
            m_strDeptName = "";            
            m_strRole = "";
            m_strDataRole = "";
            m_strCreateName = "";
            m_strCreateTime = DateTime.Now.ToString();
            m_strRemark = "";
            m_dictLimit = new Dictionary<string, string>();
        }

        /// <summary>
        /// 构造函数(值初始化)
        /// </summary>
        public void _init()
        {
            m_nId = 0;
            m_strCode = "";
            m_strName = "";
            m_strPassword = "";
            m_strRole = "";
            m_strDataRole = "";
            m_strCreateName = AppInfo.m_UserInfo.m_strName;
            m_strCreateTime = DateTime.Now.ToString();
            m_strRemark = "";
            m_dictLimit = new Dictionary<string, string>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int _saveInfo()
        {
            m_strCreateName = AppInfo.m_UserInfo.m_strName;
            int nResult = 0;
            string strSql = "";
            try
            {
                switch (m_nOperate)
                {
                    case 1:
                        strSql = @"insert into TH_User(Code,Name,DeptCode,DeptName,Password,Role,DataRole,Remark,CreateName,CreateTime,Status)
                               select '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',getDate(),1";
                        strSql = string.Format(strSql, m_strCode, m_strName, m_strDeptCode, m_strDeptName, m_strPassword,m_strRole, m_strDataRole, m_strRemark,m_strCreateName);
                        break;
                    case 2:
                        strSql = @"Update TH_User set Name = '{2}',DeptCode = '{3}',DeptName = '{4}',Password = '{5}',Role = '{6}',DataRole = '{7}',Remark = '{8}',CreateName = '{9}',CreateTime = getDate() where Id = {0}";
                        strSql = string.Format(strSql, m_nId, m_strCode,m_strName, m_strDeptCode, m_strDeptName, m_strPassword, m_strRole, m_strDataRole, m_strRemark, m_strCreateName);
                        break;
                    case 3:
                        strSql = @"delete from TH_User where Id = {0}";
                        strSql = string.Format(strSql, m_nId);
                        break;
                }
                nResult = DbHelper.DBAccess[0]._execSql(strSql);
            }
            catch
            {
            }
            return nResult;
        }

        /// <summary>
        /// 检验名称及编号
        /// </summary>
        /// <returns></returns>
        public Boolean _checkName()
        {
            Boolean bReturn = false;
            string strSql = "";
            string strInfo = "";
            strSql = "select Code from TH_User where Id <> {0} and Code = '{1}'";
            strSql = string.Format(strSql, m_nId, m_strCode);
            strInfo = CommFun._openSql_Value(strSql);
            if (!strInfo.Equals(""))
            {
                CommFun._messageBoxShow("人员编号已经存在,请重新设置!");
                return bReturn;
            }
            else
            {
                strSql = "select Name from TH_User where Id <> {0} and Name = '{1}'";
                strSql = string.Format(strSql, m_nId, m_strName);
                strInfo = CommFun._openSql_Value(strSql);
                if (!strInfo.Equals(""))
                {
                    CommFun._messageBoxShow("人员名称已经存在,请重新设置!");
                    return bReturn;
                }
            }
            bReturn = true;
            return bReturn;
        }

        /// <summary>
        /// 取信息
        /// </summary>
        /// <returns></returns>
        public void _getInfo()
        {
            string strSql = @"select top 1 * from TH_User where Id = {0} ";
            strSql = string.Format(strSql, m_nId);
            DataTable dt = CommFun._openSql_dt(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                m_nOperate = m_nId == 0 ? 1 : 2;
                m_strCode = dt.Rows[0]["Code"].ToString();
                m_strName = dt.Rows[0]["Name"].ToString();
                m_strDeptCode = dt.Rows[0]["DeptCode"].ToString();
                m_strDeptName = dt.Rows[0]["DeptName"].ToString();
                m_strPassword = dt.Rows[0]["Password"].ToString();
                m_strRole = dt.Rows[0]["Role"].ToString();
                m_strDataRole = dt.Rows[0]["DataRole"].ToString();
                m_strCreateName = dt.Rows[0]["CreateName"].ToString();
                m_strCreateTime = dt.Rows[0]["CreateTime"].ToString();    
                m_strRemark = dt.Rows[0]["Remark"].ToString();
            }
        }

        /// <summary>
        /// 身份校验
        /// </summary>
        public Boolean _checkLogin()
        {
            Boolean bReturn = false;
            string strSql = "select * from TH_User where Status = 1 and Code = '{0}' and Password = '{1}'";
            strSql = string.Format(strSql, m_strCode.Replace("'", "''"), m_strPassword.Replace("'", "''"));
            DataTable dt = CommFun._openSql_dt(strSql);
            bReturn = (dt != null) && (dt.Rows.Count > 0);
            if (bReturn)
            {
                m_nId = CommDll.FunctionComm._strToInt(dt.Rows[0]["ID"].ToString());
                m_strName = dt.Rows[0]["Name"].ToString();
                m_strDeptCode = dt.Rows[0]["DeptCode"].ToString();
                m_strDeptName = dt.Rows[0]["DeptName"].ToString();
                m_strRole = dt.Rows[0]["Role"].ToString();
            }
            // 取权限信息
            m_dictLimit.Clear();
            if (!m_strRole.Equals(""))
            {
                strSql = string.Format("select Limit from TH_System_Role where charIndex(',' + Code + ',',',{0},') > 0", m_strRole);
                dt = CommFun._openSql_dt(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    foreach (string str in dr["Limit"].ToString().Split(','))
                    {
                        if (!str.Equals("") && !m_dictLimit.ContainsKey(str))
                        {
                            m_dictLimit.Add(str, str);
                        }
                    }
                }
            }
            return bReturn;
        }

        /// <summary>
        /// 更改口令
        /// </summary>
        public Boolean _changePassword(string strPasswordNew)
        {
            Boolean bReturn = false;
            string strSql = "Update TH_User set Password = '{1}' where ID = {0}";
            strSql = string.Format(strSql, m_nId, strPasswordNew.Replace("'", "''"));
            bReturn = CommFun._execSql(strSql) > 0;            
            return bReturn;
        }
    }

    /// <summary>
    /// ClassName:System_Dept
    /// Version:1.0
    /// Date:2022-11-23
    /// Author:SeanOuyang
    /// </summary>
    /// <remarks>
    /// 本类是实体类SystemDept(部门信息)属性说明类
    /// </remarks>
    public class SystemDept
    {
        #region 1.属性
        //操作(0:查看 1:新增 2:修改 3:删除)
        public int m_nOperate { set; get; }
        //序号
        public int m_nId { set; get; }
        //用户编号
        public string m_strCode { set; get; }
        //用户姓名
        public string m_strName { set; get; }
        //操作员
        public string m_strCreateName { set; get; }
        #endregion

        /// <summary>
        /// 构造函数(值初始化)
        /// </summary>
        public SystemDept()
        {
            m_nOperate = 0;
            m_nId = 0;
            m_strCode = "";
            m_strName = "";
            m_strCreateName = AppInfo.m_UserInfo.m_strName;
        }

        /// <summary>
        /// 构造函数(值初始化)
        /// </summary>
        public void _init()
        {
            m_nId = 0;
            m_strCode = "";
            m_strName = "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int _saveInfo()
        {
            m_strCreateName = AppInfo.m_UserInfo.m_strName;
            int nResult = 0;
            string strSql = "";
            try
            {
                switch (m_nOperate)
                {
                    case 1:
                        strSql = @"insert into TH_System_Dept(Code,Name,CreateName,CreateTime)
                               select '{0}','{1}','{2}',getDate()";
                        strSql = string.Format(strSql, m_strCode, m_strName, m_strCreateName);
                        break;
                    case 2:
                        strSql = @"Update TH_System_Dept set Code = '{1}',Name = '{2}',CreateName = '{3}',CreateTime = getDate() where Id = {0}";
                        strSql = string.Format(strSql, m_nId, m_strCode, m_strName, m_strCreateName);
                        break;
                    case 3:
                        strSql = @"delete from TH_System_Dept where Id = {0}";
                        strSql = string.Format(strSql, m_nId);
                        break;
                }
                nResult = DbHelper.DBAccess[0]._execSql(strSql);
            }
            catch
            {
            }
            return nResult;
        }

        /// <summary>
        /// 检验名称及编号
        /// </summary>
        /// <returns></returns>
        public Boolean _checkName()
        {
            Boolean bReturn = false;
            string strSql = "";
            string strInfo = "";
            strSql = "select Code from TH_System_Dept where Id <> {0} and Code = '{0}'";
            strSql = string.Format(strSql, m_nId, m_strCode);
            strInfo = CommFun._openSql_Value(strSql);
            if (!strInfo.Equals(""))
            {
                CommFun._messageBoxShow("部门编号已经存在,请重新设置!");
                return bReturn;
            }
            else {
                strSql = "select Code from TH_System_Dept where Id <> {0} and Name = '{0}'";
                strSql = string.Format(strSql, m_nId, m_strName);
                strInfo = CommFun._openSql_Value(strSql);
                if (!strInfo.Equals(""))
                {
                    CommFun._messageBoxShow("部门名称已经存在,请重新设置!");
                    return bReturn;
                }
            }
            bReturn = true;
            return bReturn;
        }

        /// <summary>
        /// 取信息
        /// </summary>
        /// <returns></returns>
        public void _getInfo()
        {
            string strSql = @"select top 1 * from TH_System_Dept where Id = {0} ";
            strSql = string.Format(strSql, m_nId);
            DataTable dt = CommFun._openSql_dt(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                m_nOperate = m_nId == 0 ? 1 : 2;                
                m_strCode = dt.Rows[0]["Code"].ToString();
                m_strName = dt.Rows[0]["Name"].ToString();
            }
        }
    }

    /// <summary>
    /// ClassName:SystemRole
    /// Version:1.0
    /// Date:2022-11-23
    /// Author:SeanOuyang
    /// </summary>
    /// <remarks>
    /// 本类是实体类SystemRole(角色信息)属性说明类
    /// </remarks>
    public class SystemRole
    {
        #region 1.属性
        //操作(0:查看 1:新增 2:修改 3:删除)
        public int m_nOperate { set; get; }
        //序号
        public int m_nId { set; get; }
        //编号
        public string m_strCode { set; get; }
        //名称
        public string m_strName { set; get; }
        //权限
        public string m_strLimit { set; get; }
        //操作员
        public string m_strCreateName { set; get; }
        #endregion

        /// <summary>
        /// 构造函数(值初始化)
        /// </summary>
        public SystemRole()
        {
            m_nOperate = 0;
            m_nId = 0;
            m_strCode = "";
            m_strName = "";
            m_strLimit = "";
        }

        /// <summary>
        /// 构造函数(值初始化)
        /// </summary>
        public void _init()
        {
            m_nId = 0;
            m_strCode = "";
            m_strName = "";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int _saveInfo()
        {
            m_strCreateName = AppInfo.m_UserInfo.m_strName;
            int nResult = 0;
            string strSql = "";
            try
            {
                switch (m_nOperate)
                {
                    case 1:
                        strSql = @"insert into TH_System_Role(Code,Name,Limit,CreateName,CreateTime,Status)
                               select '{0}','{1}','{2}','{3}',getDate(),1";
                        strSql = string.Format(strSql, m_strCode, m_strName,m_strLimit, m_strCreateName);
                        break;
                    case 2:
                        strSql = @"Update TH_System_Role set Code = '{1}',Name = '{2}',Limit = '{3}',CreateName = '{4}',CreateTime = getDate() where Id = {0}";
                        strSql = string.Format(strSql, m_nId, m_strCode, m_strName,m_strLimit, m_strCreateName);
                        break;
                    case 3:
                        strSql = @"delete from TH_System_Role where Id = {0}";
                        strSql = string.Format(strSql, m_nId);
                        break;
                }
                nResult = DbHelper.DBAccess[0]._execSql(strSql);
            }
            catch
            {
            }
            return nResult;
        }

        /// <summary>
        /// 检验名称及编号
        /// </summary>
        /// <returns></returns>
        public Boolean _checkName()
        {
            Boolean bReturn = false;
            string strSql = "";
            string strInfo = "";
            strSql = "select Code from TH_System_Role where Id <> {0} and Code = '{0}'";
            strSql = string.Format(strSql, m_nId, m_strCode);
            strInfo = CommFun._openSql_Value(strSql);
            if (!strInfo.Equals(""))
            {
                CommFun._messageBoxShow("角色编号已经存在,请重新设置!");
                return bReturn;
            }
            else
            {
                strSql = "select Code from TH_System_Role where Id <> {0} and Name = '{0}'";
                strSql = string.Format(strSql, m_nId, m_strName);
                strInfo = CommFun._openSql_Value(strSql);
                if (!strInfo.Equals(""))
                {
                    CommFun._messageBoxShow("角色名称已经存在,请重新设置!");
                    return bReturn;
                }
            }
            bReturn = true;
            return bReturn;
        }

        /// <summary>
        /// 取信息
        /// </summary>
        /// <returns></returns>
        public void _getInfo()
        {
            string strSql = @"select top 1 * from TH_System_Role where Id = {0} ";
            strSql = string.Format(strSql, m_nId);
            DataTable dt = CommFun._openSql_dt(strSql);
            if (dt != null && dt.Rows.Count > 0)
            {
                m_nOperate = m_nId == 0 ? 1 : 2;
                m_strCode = dt.Rows[0]["Code"].ToString();
                m_strName = dt.Rows[0]["Name"].ToString();
                m_strLimit = dt.Rows[0]["Limit"].ToString();
            }
        }
    }
}
